﻿using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Model.WaiGaoQiao;
using WaiGaoQiaoWeb.Models;
using WaiGaoQiaoWeb.JsonEntities;
using AutoMapper;
using System.Data;

namespace WaiGaoQiaoWeb.Controllers
{
    public class ConsumerAccountController : Controller
    {
        private ConsumerAccountInfo yaxinfo = new ConsumerAccountInfo();
        public ActionResult ConsumerAccountIndex()
        {
            return View();
        }

        /// <summary>
        /// 获取账号列表
        /// </summary>
        /// <param name="pvm"></param>
        /// <returns></returns>
        public ActionResult GetList(PageVM pvm)
        {
            string query = string.Empty;
            DataSet ds = yaxinfo.GetList(pvm.rp, pvm.page);
            JsonFlexiGridData jfgd = new JsonFlexiGridData();
            jfgd.page = pvm.page;
            jfgd.total = yaxinfo.GetListCount();
            List<string> v;
            foreach (DataRow dr in ds.Tables[0].Rows)
            {
                v = new List<string>();
                v.Add(dr["userid"].ToString());
                v.Add(dr["dispname"].ToString());
                FlexiGridRow fgr = new FlexiGridRow()
                {
                    id = dr["userid"].ToString(),
                    cell = v
                };
                jfgd.rows.Add(fgr);
            }
            return Json(jfgd);
        }

        /// <summary>
        /// 删除账号
        /// </summary>
        /// <param name="roleid"></param>
        /// <returns></returns>
        public ActionResult DeleteConsumerAccount(string id)
        {
            string strId = id.Remove(0, 1);
            int result = 0;
            result = yaxinfo.DeleteConsumerAccount(strId);
            return Json(result);
        }

        public ActionResult AddConsumerAccount()
        {
            return View();
        }

        /// <summary>
        /// 保存账号（新增界面下）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveConsumerAccount(ConsumerAccountAddVM model)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                object[] yax = new object[] { model.dispname, model.passwords };
                result = yaxinfo.SaveConsumerAccount(yax);
            }
            return Json(result);
        }

        /// <summary>
        /// 点击修改按钮执行的方法
        /// </summary>
        /// <param name="supplierNo"></param>
        /// <returns></returns>
        public ActionResult EditConsumerAccount(string id)
        {
            ConsumerAccountVM vm = new ConsumerAccountVM();
            ConsumerAccountInfo info = new ConsumerAccountInfo();
            DataSet ds = info.GetConsumerAccountInfoOne(id);
            vm.userid = Convert.ToInt32(ds.Tables[0].Rows[0]["userid"]);
            vm.dispname = ds.Tables[0].Rows[0]["dispname"].ToString();
            vm.PasswordsEdit = ds.Tables[0].Rows[0]["passwords"].ToString();
            return View(vm);
        }

        /// <summary>
        /// 保存账号（修改界面下）
        /// </summary>
        /// <param name="model"></param>
        /// <returns></returns>
        public ActionResult SaveConsumerAccountEdit(ConsumerAccountVM model)
        {
            int result = 0;
            if (ModelState.IsValid)
            {
                object[] yax = new object[] { model.userid, model.dispname, model.passwords };
                result = yaxinfo.SaveConsumerAccountEdit(yax);
            }
            return Json(result);
        }
        /// <summary>
        /// 验证密码与确认密码是否相符
        /// </summary>
        /// <param name="passwords"></param>
        /// <param name="passwordsEnter"></param>
        /// <returns></returns>
        public ActionResult CheckPasswords(string passwords, string passwordsEnter)
        {
            return Json(passwords == passwordsEnter ? true : false);
        }


        /// <summary>
        /// 验证旧密码是否正确
        /// </summary>
        /// </summary>
        /// <param name="PassWordsOld"></param>
        /// <param name="PasswordsEdit"></param>
        /// <returns></returns>
        public ActionResult CheckPasswordsOld(string PassWordsOld, string PasswordsEdit)
        {
            return Json(PassWordsOld == PasswordsEdit ? true : false);
        }
    }
}
